﻿/**
 * --------------------------------------------------------------------
 * jQuizzy - jQuery plugin for creating quizzes
 * by Siddharth S - www.ssiddharth.com
 * Copyright (c) 2011 Siddharth
 * Version: 1.5	
 * --------------------------------------------------------------------
 **/

;
(function ($) {
    $.fn.jquizzy = function (settings) {
        var defaults = {
            questions: null,
        };

        var config = $.extend(defaults, settings);
        if (config.questions === null) {
            $(this).html('<div class="intro-container slide-container"><h2 class="qTitle">Failed to parse questions.</h2></div>');
            return;
        }

        var superContainer = $(this),
            answers = [],
            contentFob = '';
        superContainer.addClass('main-quiz-holder');

        for (questionsIteratorIndex = 0; questionsIteratorIndex < config.questions.length; questionsIteratorIndex++) {
            contentFob += '<div class="slide-container">' + '<div class="question">' + config.questions[questionsIteratorIndex].question + '</div><ul class="answers">';
            for (answersIteratorIndex = 0; answersIteratorIndex < config.questions[questionsIteratorIndex].answers.length; answersIteratorIndex++) {
                contentFob += '<li>' + config.questions[questionsIteratorIndex].answers[answersIteratorIndex] + '</li>';
            }

            contentFob += '</ul><div class="nav-container">';

            if (questionsIteratorIndex !== 0) {
                contentFob += '<div class="prev"><a class="nav-previous" href="#">上一题</a></div>';
            }
            contentFob += '</div></div>';
            answers.push(config.questions[questionsIteratorIndex].correctAnswer);
        }

        superContainer.html(contentFob);

        var userAnswers = [],
            questionLength = config.questions.length,
            slidesList = superContainer.find('.slide-container');

        function judgeSkills(score) {
            if (score >= 70)
                window.location.href = '../../views/evaluating/evaluating_result1.html';
            else if (score >= 40)
                window.location.href = '../../views/evaluating/evaluating_result2.html';
            else if (score < 40)
                window.location.href = '../../views/evaluating/evaluating_result3.html';
        }

        superContainer.find('li').click(function () {
            var thisLi = $(this);
            var index = thisLi.index();
            console.log(index);
            if (thisLi.hasClass('selected')) {
                thisLi.removeClass('selected');
            } else {
                thisLi.parents('.answers').children('li').removeClass('selected');
                thisLi.addClass('selected');
            }
        });
        superContainer.find('li').click(function () {
            $(this).parents('.slide-container').fadeOut(500, function () {
                $(this).next().fadeIn(500);
            });
            return false;
        });

        superContainer.find('.prev').click(function () {
            $(this).parents('.slide-container').fadeOut(500, function () {
                $(this).prev().fadeIn(500);
            });
            return false;
        });

        superContainer.find('.slide-container:last-child li').click(function () {
            superContainer.find('li.selected').each(function (index) {
                userAnswers.push($(this).parents('.answers').children('li').index($(this).parents('.answers').find('li.selected')) + 1);
                // console.log(userAnswers);
                userAnswers.forEach(function (value, index) {
                    if (value === 1) {
                        userAnswers[index] = 10;
                    } else if (value === 2) {
                        userAnswers[index] = 5;
                    } else if (value === 3) {
                        userAnswers[index] = 0;
                    }
                })
                if (userAnswers.length === 10) {
                    var score = 0;
                    for (var i = 0; i < userAnswers.length; i++) {
                        score += userAnswers[i];
                    }
                    // console.log(score);
                    judgeSkills(score);
                }
            });
        })
    };
})(jQuery);